<template>
  <div>
    <el-card class="box-card" style="padding: 20px;">
      <div class="btn">
        <el-button type="primary" @click="dialogVisible=true">添加角色</el-button>
      </div>
      <div class="table" style="padding: 20px;">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="角色" prop="name" width="180">
            <template slot-scope="{row}">
              <el-input v-if="row.isEdit" v-model="eidtList.name" />
              <span v-else>{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="启用" prop="state" width="180">
            <template slot-scope="{row}">
              <el-switch
                v-if="row.isEdit"
                v-model="eidtList.state"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ff4949"
              />
              <span v-else>{{ row.state === 1?'已启用':'未启用' }}</span>
            </template>
          </el-table-column>

          <el-table-column label="描述" prop="description">
            <template slot-scope="{row}">
              <el-input v-if="row.isEdit" v-model="eidtList.description" type="textarea" />
              <span v-else>{{ row.description }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" prop="address">
            <template slot-scope="{row}">
              <div v-if="row.isEdit">
                <el-button type="primary" :loading="load" @click="updateRoleTmp">确定</el-button>
                <el-button @click="row.isEdit = false">取消</el-button>
              </div>
              <div v-else>
                <el-button type="text">分配角色</el-button>
                <el-button type="text" @click="btnEidtRow(row)">编辑</el-button>
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="delRole(row.id)"
                >
                  <el-button slot="reference" type="text" style="margin-left: 10px;">删除</el-button>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          :current-page="paramsPage.page"
          :page-size="paramsPage.pagesize"
          :page-sizes="[3, 5, 10, 20]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" title="提示" width="30%">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        class="demo-ruleForm"
        label-width="100px"
      >
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="即时配送" prop="delivery">
          <el-switch
            v-model="ruleForm.state"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
        <el-form-item label="活动形式" prop="description">
          <el-input v-model="ruleForm.description" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="submitForm">立即创建</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
// eslint-disable-next-line no-unused-vars
import { getRolesRequest, addRoleRequest, delRoleRequest, updateRoleRequest } from '@/api/role.js'
export default {
  name: '',
  data() {
    return {
      load: false,
      dialogVisible: false,
      tableData: [],
      paramsPage: {
        page: 1,
        pagesize: 3
      },
      eidtList: [],
      total: 0,
      ruleForm: {
        name: '',
        state: 1,
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]

      },
      loading: false
    }
  },
  computed: {

  },
  created() {
    this.getRoles()
  },
  methods: {
    async getRoles() {
      const res = await getRolesRequest(this.paramsPage)
      console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
      this.tableData.forEach(item => {
        this.$set(item, 'isEdit', false)
        console.log(this.tableData)
      })
    },
    handleSizeChange(val) {
      this.paramsPage.pagesize = val
      this.getRoles()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.paramsPage.page = val
      this.getRoles()
      console.log(`当前页: ${val}`)
    },
    submitForm() {
      this.$refs.ruleForm.validate(async(valid) => {
        this.loading = true
        if (valid) {
          await addRoleRequest(this.ruleForm)
          this.loading = false
          this.resetForm()
          this.$message.success('新增角色成功')
          this.getRoles()
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
      this.dialogVisible = false
    },
    async del(id) {
      await delRoleRequest(id)
    },
    async updateRoleTmp() {
      try {
        this.load = true
        await updateRoleRequest(this.eidtList)
        await this.getRoles()
        this.load = false
      } catch (_) {
        this.load = false
      }
    },
    btnEidtRow(row) {
      this.tableData.forEach(item => {
        item.isEdit = false
      })
      row.isEdit = true
      this.eidtList = { ...row }
    },
    async delRole(id) {
      try {
        if (this.tableData.length === 1) {
          this.paramsPage.page === 1 ? this.paramsPage.page = 1 : this.paramsPage.page--
        }
        console.log(id)
        await delRoleRequest(id)
        this.getRoles()
      } catch (_) {
        //
      }
    }

  }
}
</script>
<style lang='less'  scoped>
</style>
